package top.chatgqt.SmartChargeStation.mqtt.client.factory;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ReleaseStrategy;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
import org.springframework.messaging.MessageChannel;
import org.springframework.stereotype.Component;
import top.chatgqt.SmartChargeStation.mqtt.client.conf.MqttProps;
import top.chatgqt.SmartChargeStation.mqtt.client.model.MqttConstants;

/**
 * 消息发送
 */
@EnableIntegration
@Configuration
@Slf4j
public class OutBoundSend {

    @Resource
    private MqttProps props;

    @Resource
    private FactoryBuilder factory;

    /*
     * 发送通道
     */
    @Bean(name = MqttConstants.OUT_CHANNEL)
    public MessageChannel outChannel() {
        return new DirectChannel();
    }

    /*
        创建消息处理器
        @ServiceActivator: 将消息处理器投放到指定的消息通道
     */
    @Bean
    @ServiceActivator(inputChannel = MqttConstants.OUT_CHANNEL)
    public MqttPahoMessageHandler outHandler() {
        return new MqttPahoMessageHandler(props.getClientId(), factory.getClientFactory());
    }
}
